package com.offcn.sellergood.controller;
import com.offcn.sellergood.entity.GoodsEntity;
import com.offcn.sellergood.pojo.Goods;
import com.offcn.sellergood.service.GoodsService;
import com.offcn.entity.PageResult;
import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/****
 * @Author:ujiuye
 * @Description:
 * @Date 2021/2/1 14:19
 *****/
@Api(tags = "GoodsController")
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @PostMapping
    public Result add(@RequestBody GoodsEntity goodsEntity){
        goodsService.add(goodsEntity);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @GetMapping("/{id}")
    public Result<GoodsEntity> findById(@PathVariable Long id){
        GoodsEntity goods = null;
        try {
            goods = goodsService.findById(id);
            return new Result<>(true,StatusCode.OK,"查询成功",goods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,e.getMessage());
        }
    }

    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable Long id){
        try {
            goodsService.audit(id);
            return new Result(true,StatusCode.OK,"审核通过");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }

    /**
     * 商品下架
     * @param id
     * @return
     */
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable Long id){
        try {
            goodsService.pull(id);
            return new Result(true,StatusCode.OK,"成功下架");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }

    @PutMapping("/put/{id}")
    public Result put(@PathVariable Long id){
        try {
            goodsService.put(id);
            return new Result(true,StatusCode.OK,"成功上架");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }

    @PutMapping("/put/all")
    public Result putAll(@RequestBody Long[] ids){
        int rows = goodsService.putAll(ids);
        return new Result(true,StatusCode.OK,"上架" + rows + "件商品");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable  Long id){
        try {
            goodsService.delete(id);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,e.getMessage());
        }
    }
}
